package com.ckx.dao;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ckx.base.PageResult;
import com.ckx.dao.entity.OrderDO;
import com.ckx.dto.OrderQueryDTO;
import org.apache.commons.lang3.StringUtils;
import org.dromara.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.List;
import java.util.function.Function;

/**
* @author wangjun
* @description 针对表【t_order_20231112】的数据库操作Mapper
* @createDate 2023-11-12 15:39:12
* @Entity .com.ckx.dao.TOrder20231112
*/
public interface OrderMapper extends BaseMapper<OrderDO> {

    default PageResult<OrderDO> queryPage(OrderQueryDTO orderQueryDTO) {
        Page<OrderDO> page = new Page<>(orderQueryDTO.getPageIndex(), orderQueryDTO.getPageSize());
        LambdaQueryWrapper<OrderDO> wrapper = Wrappers.lambdaQuery(OrderDO.class);
        String beginTime = orderQueryDTO.getBeginTime();
        String endTime = orderQueryDTO.getEndTime();

        String format = "yyyy-MM-dd HH:mm:ss";
        Function<String, Date> transform = s -> DateUtil.parse(s, format);

        Date startDate = transform.apply(StringUtils.rightPad(beginTime, format.length(), "00:00:00"));
        Date endDate = transform.apply(StringUtils.rightPad(endTime, format.length(), "23:59:59"));
        wrapper.gt(OrderDO::getCreateTime, startDate);
        wrapper.le(OrderDO::getCreateTime, endDate);

        Page<OrderDO> queryPage = this.selectPage(page, wrapper);
        List<OrderDO> records = queryPage.getRecords();

        PageResult<OrderDO> pageResult = new PageResult<>();
        pageResult.setPageIndex(orderQueryDTO.getPageIndex());
        pageResult.setPageSize(orderQueryDTO.getPageSize());
        pageResult.setTotalCount(queryPage.getTotal());
        pageResult.setTotalPage((int) queryPage.getPages());
        pageResult.setData(records);
        return pageResult;
    }

}




